package com.example.smallwork;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

    public class registerActivity extends AppCompatActivity {

        private EditText user1,password1,password2;
        private String userName,passWord1,passWord2;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            user1=findViewById(R.id.user1);
            password1=findViewById(R.id.password1);
            password2=findViewById(R.id.password2);
            Button b3 = (Button)findViewById(R.id.b3);
            b3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getEditText();//获取EditText控件上的文本

                    if(TextUtils.isEmpty(userName)){
                        Toast.makeText(registerActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                        return;
                    }else if(TextUtils.isEmpty(passWord1)){
                        Toast.makeText(registerActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                        return;
                    }else if(TextUtils.isEmpty(passWord2)){
                        Toast.makeText(registerActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                        return;
                    }else if(!passWord1.equals(passWord2)) {
                        Toast.makeText(registerActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                        return;
                    }else if (isExitUserName(userName)){
                        Toast.makeText(registerActivity.this,"此账户已经存在",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(registerActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        //把账号、密码和账号标识保存到sp里面
                        /**
                         * 保存账号和密码到SharedPreferences中
                         */
                        saveRegisterInfo(userName, passWord1);
                        Intent data = new Intent();
                        data.putExtra("userName", userName);
                        setResult(RESULT_OK, data);
                        registerActivity.this.finish();
                    }

                }
            });
        }

        //读取EditText中的文本内容
        private void getEditText(){
            userName=user1.getText().toString().trim();
            passWord1=password1.getText().toString().trim();
            passWord2=password2.getText().toString().trim();
        }

        //将账号密码保存到SharePreferences
        private void saveRegisterInfo(String userName,String psw){
            SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
            editor.putString(userName, psw);
            editor.commit();
        }

        //从SharePreferen中读取用户名，判断用户名是否已经被注册
        private boolean isExitUserName(String userName){
            boolean exit_userName=false;
            SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
            String psw=sp.getString(userName,"");

            if(!TextUtils.isEmpty(psw)){
                exit_userName=true;
            }
            return exit_userName;
        }

    }
